<template>
  <el-dialog
    :title="title"
    width="700px"
    :visible.sync="show"
    @close="closeEvent"
  >
    <!-- el-form   label-width
               el-form-item   label
            -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item prop="name" label="部门名称1">
        <el-input v-model.trim="form.name" placeholder="1-50个字符" />
      </el-form-item>

      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <el-select v-model="form.manager" @focus="getUserList">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input
          v-model="form.introduce"
          type="textarea"
          placeholder="1-300个字符"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button>取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import {
  sysUserSimple,
  companyDepartmentPost,
  companyDepartmentPut
} from '@/api/departments'
export default {
  props: {
    treelist: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      mode: 'add',
      show: false,
      userList: [], // 员工列表
      form: {
        name: '', //  string  非必须    部门名称
        code: '', //  string  非必须    部门编码，同级部门不可重复
        manager: '', //  string  非必须    负责人名称
        introduce: '', //  string  非必须    介绍
        pid: '' //  string  非必须    父级部门ID
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 1,
            max: 50,
            message: '请输入1-50个字符',
            trigger: 'change'
          },
          {
            validator: (rule, value, callback) => {
              let arr = this.treelist.filter(
                (item) => item.pid === this.form.pid
              )
              if (this.mode === 'edit') {
                arr = arr.filter((item) => item.id !== this.form.id)
              }
              const bol = arr.every((item) => item.name !== value)
              bol
                ? callback()
                : callback(new Error('请不要输入重复的部门名称:' + value))
            },
            trugger: 'change'
          }
        ], //  string  非必须    部门名称
        code: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let bol = false
              bol = this.treelist.every((item) => {
                return item.code !== value
              })

              if (this.mode === 'edit') {
                const arr = this.treelist.filter((item) => {
                  return item.id !== this.form.id
                })
                bol = arr.every((item) => {
                  return item.code !== value
                })
              }

              bol ? callback() : callback(new Error('重复的值' + value))
            }
          }
        ], //  string  非必须    部门编码，同级部门不可重复
        manager: [{ required: true, message: '必填', trigger: 'change' }], //  string  非必须    负责人名称
        introduce: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 300, message: '请输入1-300个字符', trigger: 'change' }
        ] //  string  非必须    介绍
      }
    }
  },
  computed: {
    title() {
      return this.mode === 'add' ? '新增部门' : '编辑部门'
    }
  },

  methods: {
    // 获取员工简单列表
    async getUserList() {
      if (this.userList.length === 0) {
        const res = await sysUserSimple()
        this.userList = res.data
      }
    },
    // // 确定点击
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          this.mode === 'add'
            ? await companyDepartmentPost(this.form)
            : await companyDepartmentPut(this.form)

          // 刷新父级列表数据
          this.$message.success(this.title + '成功')
          this.show = false
          this.$emit('getlist')
        }
      })
    },
    // 关闭dialog 清空
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    }
  }
}
</script>
<style></style>
